<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../resources/common.js"></script>
</head>
<body>
<script>
description('Check stepping-up and -down for time input fields from renderer. No cases of empty initial values.');
if (!window.internals)
    testFailed('This test requires window.internals.');

var input = document.createElement('input');
document.body.appendChild(input);

function keyDown(key, modifiers)
{
    if (!window.eventSender)
        return;
    eventSender.keyDown(key, modifiers);
}

function maybeUpdateAttribute(name, value) {
    if (value) {
        if (input.getAttribute(name) != value)
            input.setAttribute(name, value);
    } else {
        if (input.hasAttribute(name))
            input.removeAttribute(name);
    }
}

function setInputAttributes(value, min, max, step) {
    input.value = value;
    maybeUpdateAttribute('min', min);
    maybeUpdateAttribute('max', max);
    maybeUpdateAttribute('step', step);
}

function test(value, step, min, max, keySequence) {
    setInputAttributes(value, min, max, step);
    for (var i = 0; i < keySequence.length; i++)
        keyDown(keySequence[i]);
    return input.value;
}

function stepUp(value, step, min, max) {
    return test(value, step, min, max, ['ArrowUp']);
}

function stepDown(value, step, min, max) {
    return test(value, step, min, max, ['ArrowDown']);
}

input.type = 'time';
input.focus();
keyDown('ArrowRight');
debug('Function arguments are (value, step, min, max, [keySequence]).');
debug('Normal cases');
shouldBeEqualToString('stepUp("07:13", null, null, null)', '07:14');
shouldBeEqualToString('stepDown("07:13", null, null, null)', '07:12');
shouldBeEqualToString('test("07:13", null, null, null, ["ArrowUp", "ArrowUp", "ArrowUp"])', '07:16');
shouldBeEqualToString('test("07:13", null, null, null, ["ArrowDown", "ArrowDown", "ArrowDown"])', '07:10');

debug('Milliseconds');
input.step = 0.001;
keyDown('ArrowRight');
keyDown('ArrowRight');
shouldBeEqualToString('stepUp("07:13:00.000", 0.001, null, null)', '07:13:00.001');
shouldBeEqualToString('stepDown("07:13:00.001", 0.001, null, null)', '07:13:00.000');
shouldBeEqualToString('stepUp("07:13:00.999", 0.001, null, null)', '07:13:00.000');
shouldBeEqualToString('stepDown("07:13:00.000", 0.001, null, null)', '07:13:00.999');
shouldBeEqualToString('stepUp("07:13:00.000", 0.250, null, null)', '07:13:00.250');
shouldBeEqualToString('stepDown("07:13:00.500", 0.250, null, null)', '07:13:00.250');
shouldBeEqualToString('stepUp("07:13:00.750", 0.250, null, null)', '07:13:00.000');
shouldBeEqualToString('stepDown("07:13:00.000", 0.250, null, null)', '07:13:00.750');
shouldBeEqualToString('stepUp("07:13:00.099", 0.100, null, null)', '07:13:00.100');
shouldBeEqualToString('stepDown("07:13:00.101", 0.100, null, null)', '07:13:00.100');
shouldBeEqualToString('stepUp("07:13:00.500", 0.0001, null, null)', '07:13:00.501');
shouldBeEqualToString('stepDown("07:13:00.500", 0.0001, null, null)', '07:13:00.499');
shouldBeEqualToString('stepUp("07:13:00.500", 0, null, null)', '07:13:00.000');
shouldBeEqualToString('stepDown("07:13:00.500", 0, null, null)', '07:13:00.000');
shouldBeEqualToString('stepUp("07:13:00.500", 60, null, null)', '07:13:00.000');
shouldBeEqualToString('stepDown("07:13:00.500", 60, null, null)', '07:13:00.000');
shouldBeEqualToString('stepUp("07:13:00.500", 0.100, "00:00:00.050", null)', '07:13:00.550');
shouldBeEqualToString('stepDown("07:13:00.500", 0.100, "00:00:00.050", null)', '07:13:00.450');
shouldBeEqualToString('test("07:13:00.500", 0.100, null, null, ["Delete", "ArrowUp"])', '07:13:00.000');
shouldBeEqualToString('test("07:13:00.500", 0.100, null, null, ["Delete", "ArrowDown"])', '07:13:00.900');
shouldBeEqualToString('test("07:13:00.500", 0.100, "00:00:00.050", null, ["Delete", "ArrowUp"])', '07:13:00.050');
shouldBeEqualToString('test("07:13:00.500", 0.100, "00:00:00.050", null, ["Delete", "ArrowDown"])', '07:13:00.950');
shouldBeEqualToString('stepUp("07:13:00.010", 0.001, "07:13:00.005", "07:13:00.010")', '07:13:00.005');
shouldBeEqualToString('stepDown("07:13:00.005", 0.001, "07:13:00.005", "07:13:00.010")', '07:13:00.010');
shouldBeEqualToString('stepUp("07:13:00.005", 0.001, "07:13:00.010", "07:13:00.020")', '07:13:00.010');
shouldBeEqualToString('stepDown("07:13:00.005", 0.001, "07:13:00.010", "07:13:00.020")', '07:13:00.020');
shouldBeEqualToString('stepUp("07:13:00.005", 0.001, "07:13:00.002", "07:13:00.002")', '07:13:00.002');
shouldBeEqualToString('stepDown("07:13:00.005", 0.001, "07:13:00.002", "07:13:00.002")', '07:13:00.002');
shouldBeEqualToString('stepUp("07:13:00.035", 0.01, "07:13:00.010", "07:13:00.035")', '07:13:00.010');
shouldBeEqualToString('stepDown("07:13:00.035", 0.01, "07:13:00.010", "07:13:00.035")', '07:13:00.030');
shouldBeEqualToString('stepUp("07:13:00.010", 0.001, "07:13:00.010", "07:13:01.020")', '07:13:00.011');
shouldBeEqualToString('stepDown("07:13:00.010", 0.001, "07:13:00.010", "07:13:01.020")', '07:13:00.009');

debug('Seconds');
keyDown('ArrowLeft');
shouldBeEqualToString('stepUp("07:13:00", 1, null, null)', '07:13:01');
shouldBeEqualToString('stepDown("07:13:01", 1, null, null)', '07:13:00');
shouldBeEqualToString('stepUp("07:13:59", 0.001, null, null)', '07:13:00.000');
shouldBeEqualToString('stepDown("07:13:00", 0.001, null, null)', '07:13:59.000');
shouldBeEqualToString('stepUp("07:13:00", 10, null, null)', '07:13:10');
shouldBeEqualToString('stepDown("07:13:30", 10, null, null)', '07:13:20');
shouldBeEqualToString('stepUp("07:13:50", 10, null, null)', '07:13:00');
shouldBeEqualToString('stepDown("07:13:00", 10, null, null)', '07:13:50');
shouldBeEqualToString('stepUp("07:13:09", 10, null, null)', '07:13:10');
shouldBeEqualToString('stepDown("07:13:11", 10, null, null)', '07:13:10');
shouldBeEqualToString('stepUp("07:13:30", 2.5, null, null)', '07:13:31.000');
shouldBeEqualToString('stepDown("07:13:30", 2.5, null, null)', '07:13:29.000');
shouldBeEqualToString('stepUp("07:13:30", 0, null, null)', '07:13:00');
shouldBeEqualToString('stepDown("07:13:30", 0, null, null)', '07:13:00');
shouldBeEqualToString('stepUp("07:13:30", 3600, null, null)', '07:13:00');
shouldBeEqualToString('stepDown("07:13:30", 3600, null, null)', '07:13:00');
shouldBeEqualToString('stepUp("07:13:30", 11, null, null)', '07:13:31');
shouldBeEqualToString('stepDown("07:13:30", 11, null, null)', '07:13:29');
shouldBeEqualToString('stepUp("07:13:30", 10, "00:00:05", null)', '07:13:35');
shouldBeEqualToString('stepDown("07:13:30", 10, "00:00:05", null)', '07:13:25');
shouldBeEqualToString('test("07:13:30", 10, null, null, ["Delete", "ArrowUp"])', '07:13:00');
shouldBeEqualToString('test("07:13:30", 10, null, null, ["Delete", "ArrowDown"])', '07:13:50');
shouldBeEqualToString('test("07:13:30", 10, "00:00:05", null, ["Delete", "ArrowUp"])', '07:13:05');
shouldBeEqualToString('test("07:13:30", 10, "00:00:05", null, ["Delete", "ArrowDown"])', '07:13:55');
shouldBeEqualToString('stepUp("07:13:10", 1, "07:13:05", "07:13:10")', '07:13:05');
shouldBeEqualToString('stepDown("07:13:05", 1, "07:13:05", "07:13:10")', '07:13:10');
shouldBeEqualToString('stepUp("07:13:05", 1, "07:13:10", "07:13:20")', '07:13:10');
shouldBeEqualToString('stepDown("07:13:05", 1, "07:13:10", "07:13:20")', '07:13:20');
shouldBeEqualToString('stepUp("07:13:05", 1, "07:13:02", "07:13:02")', '07:13:02');
shouldBeEqualToString('stepDown("07:13:05", 1, "07:13:02", "07:13:02")', '07:13:02');
shouldBeEqualToString('stepUp("07:13:35", 10, "07:13:10", "07:13:35")', '07:13:10');
shouldBeEqualToString('stepDown("07:13:35", 10, "07:13:10", "07:13:35")', '07:13:30');
shouldBeEqualToString('stepUp("07:13:10", 1, "07:13:10", "07:14:20")', '07:13:11');
shouldBeEqualToString('stepDown("07:13:10", 1, "07:13:10", "07:14:20")', '07:13:09');

debug('Minutes');
keyDown('ArrowLeft');
shouldBeEqualToString('stepUp("07:13", 1, null, null)', '07:14:00');
shouldBeEqualToString('stepDown("07:13", 1, null, null)', '07:12:00');
shouldBeEqualToString('stepUp("07:59", 0.001, null, null)', '07:00:00.000');
shouldBeEqualToString('stepDown("07:00", 0.001, null, null)', '07:59:00.000');
shouldBeEqualToString('stepUp("07:00", 600, null, null)', '07:10');
shouldBeEqualToString('stepDown("07:10", 600, null, null)', '07:00');
shouldBeEqualToString('stepUp("07:50", 600, null, null)', '07:00');
shouldBeEqualToString('stepDown("07:00", 600, null, null)', '07:50');
shouldBeEqualToString('stepUp("07:13", 600, null, null)', '07:20');
shouldBeEqualToString('stepDown("07:13", 600, null, null)', '07:10');
shouldBeEqualToString('stepUp("07:13", 150, null, null)', '07:14:00');
shouldBeEqualToString('stepDown("07:13", 150, null, null)', '07:12:00');
shouldBeEqualToString('stepUp("07:13", 0, null, null)', '07:14');
shouldBeEqualToString('stepDown("07:13", 0, null, null)', '07:12');
shouldBeEqualToString('stepUp("07:13", 36000, null, null)', '07:00');
shouldBeEqualToString('stepDown("07:13", 36000, null, null)', '07:00');
shouldBeEqualToString('stepUp("07:13", 660, null, null)', '07:14');
shouldBeEqualToString('stepDown("07:13", 660, null, null)', '07:12');
shouldBeEqualToString('stepUp("07:13", 600, "00:05", null)', '07:15');
shouldBeEqualToString('stepDown("07:13", 600, "00:05", null)', '07:05');
shouldBeEqualToString('test("07:13", 600, null, null, ["Delete", "ArrowUp"])', '07:00');
shouldBeEqualToString('test("07:13", 600, null, null, ["Delete", "ArrowDown"])', '07:50');
shouldBeEqualToString('test("07:13", 600, "00:05", null, ["Delete", "ArrowUp"])', '07:05');
shouldBeEqualToString('test("07:13", 600, "00:05", null, ["Delete", "ArrowDown"])', '07:55');
shouldBeEqualToString('stepUp("07:10", 1, "07:05", "07:10")', '07:05:00');
shouldBeEqualToString('stepDown("07:05", 1, "07:05", "07:10")', '07:10:00');
shouldBeEqualToString('stepUp("07:05", 1, "07:10", "07:20")', '07:10:00');
shouldBeEqualToString('stepDown("07:05", 1, "07:10", "07:20")', '07:20:00');
shouldBeEqualToString('stepUp("07:05", 1, "07:00", "07:00")', '07:00:00');
shouldBeEqualToString('stepDown("07:05", 1, "07:00", "07:00")', '07:00:00');
shouldBeEqualToString('stepUp("07:35", 600, "07:10", "07:35")', '07:10');
shouldBeEqualToString('stepDown("07:35", 600, "07:10", "07:35")', '07:30');
shouldBeEqualToString('stepUp("07:10", 1, "07:10", "08:20")', '07:11:00');
shouldBeEqualToString('stepDown("07:10", 1, "07:10", "08:20")', '07:09:00');

debug('');
document.body.removeChild(input);
</script>
</body>
</html>
